package com.taofuxn.yycreazy.adpater;

import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.taofuxn.yycreazy.MyApp;
import com.taofuxn.yycreazy.R;
import com.taofuxn.yycreazy.adpater.base.TaofuRecyclerAdapter;
import com.taofuxn.yycreazy.adpater.base.TaofuRecyclerViewHodler;
import com.taofuxn.yycreazy.database.dao.CollectionDao;
import com.taofuxn.yycreazy.database.dao.DaoSession;
import com.taofuxn.yycreazy.database.model.Collection;
import com.taofuxn.yycreazy.mvp.model.NewsertJokBean;
import com.taofuxn.yycreazy.ui.activity.DisplayImgActivity;
import com.taofuxn.yycreazy.ui.view.FlipImageView;
import com.taofuxn.yycreazy.util.MyLog;

import java.util.ArrayList;

/**
 * Created by wanghuafu on 16/9/17.
 */
public class GifJokAdapter extends TaofuRecyclerAdapter<NewsertJokBean.NewsertJokResult.NewsertJok> {

    private DaoSession mDaoSession;

    public GifJokAdapter(int layoutId, ArrayList<NewsertJokBean.NewsertJokResult.NewsertJok> mData) {
        super(layoutId, mData);
        mDaoSession = MyApp.getInstance().getmDaoUtil().getDaoSession();
    }

    @Override
    protected void convert(final TaofuRecyclerViewHodler holder, final NewsertJokBean.NewsertJokResult.NewsertJok b) {
        if (!TextUtils.isEmpty(b.updatetime))
            holder.setText(R.id.updateTime, Html.fromHtml("<font color=\"#FF0000\">[更新于 " + b.updatetime + "]</font>").toString());
        holder.setText(R.id.content, b.content);

        if (!TextUtils.isEmpty(b.url)) {
            holder.retrieveView(R.id.itemImg).setVisibility(View.VISIBLE);
            Glide.with(holder.itemView.getContext()).load(b.url)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into((ImageView) holder.retrieveView(R.id.itemImg));
            holder.retrieveView(R.id.itemImg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(holder.itemView.getContext(), DisplayImgActivity.class);
                    intent.putExtra("url", b.url);
                    holder.itemView.getContext().startActivity(intent);
                }
            });
        } else {
            holder.retrieveView(R.id.itemImg).setVisibility(View.GONE);
        }


        final FlipImageView collec = holder.retrieveView(R.id.btn_collection);
        final int rt = mDaoSession.getCollectionDao().queryBuilder().where(CollectionDao.Properties.HashId.eq(b.hashId)).build().list().size();
        if (rt > 0) {
            collec.setFlipped(true);
        } else {
            collec.setFlipped(false);
        }
        collec.setOnFlipListener(new FlipImageView.OnFlipListener() {
            @Override
            public void onClick(FlipImageView view) {
                Collection c = new Collection();
                if (!TextUtils.isEmpty(b.hashId))
                    c.setHashId(b.hashId);
                if (!TextUtils.isEmpty(b.content))
                    c.setContent(b.content);
                if (!TextUtils.isEmpty(b.unixtime))
                    c.setUnixtime(b.unixtime);
                if (!TextUtils.isEmpty(b.url))
                    c.setUrl(b.url);
                if (!TextUtils.isEmpty(b.updatetime))
                    c.setUpdatetime(b.updatetime);
                MyLog.showLog("----" + rt);
                if (rt > 0) {
                    mDaoSession.getCollectionDao().delete(c);
                } else {
                    mDaoSession.getCollectionDao().insert(c);
                }
            }

            @Override
            public void onFlipStart(FlipImageView view) {

            }
            @Override
            public void onFlipEnd(FlipImageView view) {

            }
        });

    }


}
